<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Concerns\FromCollection;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;

class TeacherInfoExport implements FromCollection,WithStyles
{
    private $data;

    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        return collect($this->data);
    }

    public function styles(Worksheet $sheet)
    {
        // 合并单元格
        $sheet->mergeCells('A1:H1');
        $sheet->mergeCells('A4:A5');
        $sheet->mergeCells('B4:C5');
        $sheet->mergeCells('D4:D5');
        $sheet->mergeCells('E4:E5');
        $sheet->mergeCells('G4:H4');
        $sheet->mergeCells('G5:H5');
        $sheet->mergeCells('B6:H6');
        $sheet->mergeCells('B7:H7');
        $sheet->mergeCells('B8:H8');
        $sheet->mergeCells('B9:H9');
        $sheet->mergeCells('B10:H10');
        $sheet->mergeCells('B11:H11');
        $sheet->mergeCells('B12:H12');
        $sheet->mergeCells('B13:H13');
        $sheet->mergeCells('G14:H14');

        // 设置单元格的值
        // $sheet->getCell('A1')->setValue("汇师小学 2022学年第一学期 教师任职情况");
        // 设置字体颜色
        // $sheet->getStyle('B5')->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
        // 设置背景色
        // $sheet->getStyle('A5:E5')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setRGB('e9e9e9');
        // 设置列宽
        $sheet->getColumnDimension('A')->setWidth(15);
        $sheet->getColumnDimension('B')->setWidth(12);
        $sheet->getColumnDimension('C')->setWidth(9);
        $sheet->getColumnDimension('D')->setWidth(10);
        $sheet->getColumnDimension('E')->setWidth(9);
        $sheet->getColumnDimension('F')->setWidth(10);
        $sheet->getColumnDimension('G')->setWidth(9);
        $sheet->getColumnDimension('H')->setWidth(12);
        // 设置行高
        $sheet->getRowDimension('1')->setRowHeight(60);
        $sheet->getRowDimension('2')->setRowHeight(26);
        $sheet->getRowDimension('3')->setRowHeight(26);
        $sheet->getRowDimension('4')->setRowHeight(26);
        $sheet->getRowDimension('5')->setRowHeight(26);
        $sheet->getRowDimension('6')->setRowHeight(60);
        $sheet->getRowDimension('7')->setRowHeight(65);
        $sheet->getRowDimension('8')->setRowHeight(60);
        $sheet->getRowDimension('9')->setRowHeight(65);
        $sheet->getRowDimension('10')->setRowHeight(60);
        $sheet->getRowDimension('11')->setRowHeight(70);
        $sheet->getRowDimension('12')->setRowHeight(60);
        $sheet->getRowDimension('13')->setRowHeight(60);
        $sheet->getRowDimension('14')->setRowHeight(26);
        // 设置默认行高
        $sheet->getDefaultRowDimension()->setRowHeight(40);
        // setWrapText自动换行，setVertical垂直对齐方式
        $sheet->getStyle('A1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
        $sheet->getStyle('A1')->getFont()->setSize(22)->setBold(true);
        $sheet->getStyle('A3:H3')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
        $sheet->getStyle('A4')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
        $sheet->getStyle('B4')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT)->setVertical(Alignment::VERTICAL_CENTER)->setWrapText(true);
        $sheet->getStyle('D4:E4')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
        $sheet->getStyle('F4:F5')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
        $sheet->getStyle('G4:G5')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
        $sheet->getStyle('A6:A13')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER)->setWrapText(true);
        $sheet->getStyle('B6:H13')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT)->setVertical(Alignment::VERTICAL_CENTER)->setWrapText(true);
        //设置边框
        $sheet->getStyle('A3:H13')->getBorders()->getAllBorders()->setBorderStyle(Border::BORDER_THIN);
    }
}
